<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2023-11-06 17:09:40
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Models;

use App\Traits\CarryTrait;
use App\Traits\CarTrait;
use App\Traits\AttachmentTrait;
use App\Traits\StorageTrait;
use App\Traits\VoucherTrait;


class PurchaseStorageDetails extends BaseModel
    {
    use StorageTrait;
    use CarryTrait;
    use CarTrait;
    use AttachmentTrait;
    use VoucherTrait;
    public $timestamps = true;
    public $table = "sys_purchase_storage_details";
    protected $guarded = [];
    //派车装卸生成单据的CODE
    public $sourceCodeField = ['main' => ["code" => ""]];
    public function newQuery()
        {
        $query = parent::newQuery();
        if ($this->_instorage_way_id)
            $query->where('instorage_way_id', $this->_instorage_way_id);
        return $query;
        }
    public function tax()
        {
        return $this->belongsTo(Dic::class, "tax_id", "key")->where(function ($q)
            {
            $q->where("code", 'FinanceTax');
            });
        }
    public function getSourceAddr()
        {
        return $this->company->name;
        }
    //运输派车目的地址
    public function getTargetAddr()
        {
        return $this->target_id == 1 ? $this->location->name : $this->outfactory->name;
        }
    public function _creating($model)
        {
        if ($this->_instorage_way_id)
            $model->instorage_way_id = $this->_instorage_way_id;

        $model->se_amount = $model->amount ?? 0;
        $model->se_price  = $model->price ?? 0;
        }

    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'status_with_finance');
            });
        }
    public function main()
        {
        return $this->belongsTo(PurchaseStorage::class, "main_id");
        }
    public function purchase_order()
        {
        return $this->belongsTo(PurchaseOrderDetails::class);
        }

    public function outfactory()
        {
        return $this->belongsTo(Company::class);
        }
    public function out_sourcing()
        {
        return $this->hasOne(ProOutsourcingDeliveryDetail::class, "relation_id");
        }
    public function instorage_way()
        {
        return $this->belongsTo(InstorageWay::class);
        }
    }
